/**
 * 财务封账列表
 * 待初审、复审等相关操作
 *
*/
import { paramsFormat } from '@/utils/paramsFormat';
import { showModal } from '@/utils/message';
export default {
  data() {
    return {
      isBatchAll: false,
      requestType: 'submitReview',
      exportLoading: false,
      idKey: 'id',
      ids: ''
    };
  },
  methods: {
    queryEmpty() {
      this.paramsObj = {};
      this.isBatchAll = false;
      this.requestType = 'submitReview';
      this.getData(true);
    },
    sortMethod(a) {
      if (a.order) {
        this.sortName = a.prop;
        this.sortType = a.order === 'ascending' ? 1 : -1;
      } else {
        this.sortName = '';
        this.sortType = '';
      }
      this.getData();
    },
    handleCurrentChange(toPage) {
      this.page = toPage;
      this.getData();
    },
    handleSizeChange(perPage) {
      this.perPage = perPage;
      this.getData(true);
    },
    handleSelectionChange(value) {
      this.multipleSelection = value;
    },
    tableRowClassName({ row, rowIndex }, extendClass = '') {
      if (this.multipleSelection && this.multipleSelection.length > 0) {
        const ids = this.multipleSelection.map(i => i.id);
        return ids.includes(row.id) ? `table-selected ${extendClass}` : extendClass;
      }
      return extendClass;
    },
    refreshData() {
      this.$emit('refreshData');
    },
    // 重新请求列表数据或者只重置加载状态
    queryClick({ isBatchAll, type, listLoadingReset } = {}) {
      if (listLoadingReset) {
        this.listLoadingReset();
      } else {
        this.isBatchAll = isBatchAll;
        this.requestType = type;
        this.page = 1;
        this.getData();
      }
    },
    // 重置表格加载状态
    listLoadingReset() {
      this.listLoading = false;
    },
    // 批量操作事件 先根据条件搜索出对应的id数组，再将id集合传送给操作接口
    operateHandle(requestType = 'submitReview', res = {}) {
      const { data: { items }} = res;
      switch (requestType) {
        case 'submitReview':
          this.$refs.tableFooterBtn.submitReview(items);
          break;
        case 'cancelJoin':
          this.$refs.tableFooterBtn.cancelJoinRequest(items);
          break;
        case 'batchBackOut':
          this.$refs.tableFooterBtn.batchBackOutRequest(items);
          break;
        case 'batchPass':
          this.$refs.tableFooterBtn.batchPassRequest(items);
          break;
        case 'batchFail':
          this.$refs.tableFooterBtn.batchFailRequest(items);
          break;
        case 'batchSeal':
          this.$refs.tableFooterBtn.storeBatchSeal(items);
          break;
        default:
          this.$refs.tableFooterBtn.submitReview(items);
          break;
      }
    },
    paramsRangeFormat(rangeKeyAttr = [], params = {}) {
      rangeKeyAttr.forEach(i => {
        const { attr, start, end, type } = i;
        if (params[attr] && params[attr].length === 2) {
          if (type === 'money') {
            params[start] = params[attr][0] || params[attr][0] === 0 ? (Number(params[attr][0]).toFixed(2) * 100).toFixed(0) : null;
            params[end] = params[attr][1] || params[attr][1] === 0 ? (Number(params[attr][1]).toFixed(2) * 100).toFixed(0) : null;
          } else {
            params[start] = params[attr][0];
            params[end] = params[attr][1];
          }
          delete params[attr];
        }
      });
    },
    // 请求参数处理
    formatParams(sendPageConfig = true) {
      const { sortName, sortType, paramsObj, dateDataAttrName, page, perPage, rangeKeyAttr,
        processState, sealingId } = this;
      let params = JSON.parse(JSON.stringify(paramsObj));
      // 时间处理
      this.paramsDateFormat(dateDataAttrName, params, 's');
      this.paramsRangeFormat(rangeKeyAttr, params);
      // 参数处理
      params = paramsFormat(params);
      if (sendPageConfig) {
        params.page = page;
        params.perPage = perPage;
      }
      if (sortName) {
        params.sortName = sortName;
        params.sortType = sortType;
      }
      params.processState = processState;
      params.id = sealingId;
      return params;
    },
    // 导出操作
    exportBeforeExport(exportRef) {
      showModal('请确认是否导出封账数据', '', {
        center: true,
        showClose: true
      }).then(() => {
        const params = this.formatParams(false);
        params.ids = this.ids;
        if (this.$refs[exportRef]) {
          this.exportLoading = true;
          this.$refs[exportRef].btnExport(params);
        }
      });
    },
    // 导出完成后触发
    exportComplete() {
      this.exportLoading = false;
    },
    getColor(status) {
      // 审批状态： 0: 待审批 1: 通过 2: 未过审
      let color = '';
      switch (status) {
        case 0:
          color = '#FD8546';
          break;
        case 1:
          color = '#31CC9C';
          break;
        case 2:
          color = '#FF4E5F';
          break;
      }
      return color;
    }
  }
};
